<template>
	<view class="container">
		<!-- 顶部标题区域 -->
		<view class="header">
			<text class="title">在校生专属论坛</text>
			<text class="subtitle">选择您当前就读的学校</text>

			<!-- 学校选择下拉框 -->
			<picker mode="selector" :range="schools" range-key="name" @change="changeSchool" class="school-picker">
				<view class="picker-content">
					<text>{{ currentSchool.name }}</text>
					<uni-icons type="arrowdown" size="16" color="#fff"></uni-icons>
				</view>
			</picker>
		</view>

		<!-- 主要内容区域 -->
		<view class="content">
			<!-- 卡通人物形象 -->
			<view class="character-container">
				<image class="character male" src="/static/avatar/1.jpg"
					mode="aspectFit"></image>
				<image class="character female" src="/static/avatar/2.jpg"
					mode="aspectFit"></image>
			</view>

			<!-- 功能提示 -->
			<view class="function-intro">
				<text class="intro-title">在这里，你可以</text>
				<view class="function-tags">
					<text class="tag">表白</text>
					<text class="tag">拼车</text>
					<text class="tag">吐槽</text>
					<text class="tag">二手交易</text>
					<text class="tag">发跑腿代买</text>
				</view>
			</view>

			<!-- 热门话题气泡 -->
			<view class="bubbles-container">
				<view v-for="(bubble, index) in bubbles" :key="index" class="bubble" :style="{
					transform: `rotateZ(${index % 2 === 0 ? '5deg' : '-5deg'})`,
					left: `${10 + (index % 3) * 30}%`,
					top: `${index * 15 + 10}%`
				  }" @click="navigateToTopic(bubble.type)">
					<text class="bubble-text">{{ bubble.text }}</text>
				</view>
			</view>
		</view>

		<!-- 底部操作栏 -->
		<view class="footer">
			<button class="action-btn" @click="navigateTo('business')">
				<uni-icons type="hand-up" size="20" color="#fff"></uni-icons>
				<text>商务合作</text>
			</button>
			<button class="action-btn primary" @click="navigateTo('publish')">
				<uni-icons type="plus" size="20" color="#fff"></uni-icons>
				<text>发布内容</text>
			</button>
		</view>

		<!-- 学校搜索弹窗 -->
		<uni-popup ref="schoolPopup" type="bottom" v-if="false">
			<view class="school-popup">
				<view class="popup-header">
					<text class="popup-title">选择学校</text>
					<button class="cancel-btn" @click="closeSchoolPopup">取消</button>
				</view>

				<view class="search-box">
					<uni-icons type="search" size="16" color="#999"></uni-icons>
					<input type="text" placeholder="搜索学校名称，至少输入全称前四个字" v-model="searchText" @input="searchSchools" />
				</view>

				<scroll-view class="school-list" scroll-y>
					<view v-for="(school, index) in filteredSchools" :key="index" class="school-item"
						@click="selectSchool(school)">
						<text>{{ school.name }}</text>
						<uni-icons v-if="currentSchool.id === school.id" type="checkmark" size="16"
							color="#1989fa"></uni-icons>
					</view>
				</scroll-view>
			</view>
		</uni-popup>
	</view>
</template>

<script setup>
	import {
		ref,
		computed
	} from 'vue'

	// 模拟数据
	const schools = ref([{
			id: 1,
			name: '皖西学院'
		},
		{
			id: 2,
			name: '安徽大学'
		},
		{
			id: 3,
			name: '合肥工业大学'
		},
		{
			id: 4,
			name: '中国科学技术大学'
		},
		{
			id: 5,
			name: '安徽师范大学'
		},
		{
			id: 6,
			name: '安徽理工大学'
		},
		{
			id: 7,
			name: '安徽农业大学'
		},
		{
			id: 8,
			name: '安徽医科大学'
		}
	])

	const bubbles = ref([{
			type: 'love',
			text: '表白你喜欢的Ta',
			emoji: '❤️'
		},
		{
			type: 'market',
			text: '区超市是真滴贵',
			emoji: '🛒'
		},
		{
			type: 'teacher',
			text: '声乐老师真滴漂亮',
			emoji: '🎤'
		},
		{
			type: 'dorm',
			text: '奇葩舍友笑死人',
			emoji: '😂'
		},
		{
			type: 'study',
			text: '四六级寻求搭子',
			emoji: '📚'
		},
		{
			type: 'photo',
			text: '分享一波校园美照',
			emoji: '📷'
		},
		{
			type: 'single',
			text: '让我看看谁还是单身狗',
			emoji: '🐶'
		}
	])

	// 响应式数据
	const currentSchool = ref(schools.value[0])
	const searchText = ref('')
	const filteredSchools = ref([...schools.value])
	const schoolPopup = ref(null)

	// 计算属性
	const showSearchResult = computed(() => {
		return searchText.value.length >= 4
	})

	// 方法
	const changeSchool = (e) => {
		currentSchool.value = schools.value[e.detail.value]
	}

	const openSchoolPopup = () => {
		schoolPopup.value.open()
	}

	const closeSchoolPopup = () => {
		schoolPopup.value.close()
	}

	const searchSchools = () => {
		if (searchText.value.length >= 4) {
			filteredSchools.value = schools.value.filter(school =>
				school.name.includes(searchText.value)
			)
		} else {
			filteredSchools.value = [...schools.value]
		}
	}

	const selectSchool = (school) => {
		currentSchool.value = school
		closeSchoolPopup()
	}

	const navigateToTopic = (type) => {
		uni.navigateTo({
			url: `/pages/topic/${type}`
		})
	}

	const navigateTo = (page) => {
		if (page === 'business') {
			uni.navigateTo({
				url: '/pages/index/index'
			})
		} else if (page === 'publish') {
			uni.navigateTo({
				url: '/pages/index/index'
			})
		}
	}
</script>

<style lang="scss">
	.container {
		display: flex;
		flex-direction: column;
		height: 95vh;
		background-color: #aaaaff;

		.header {
			background-color: #8a2be2; // 紫色背景
			padding: 40rpx 30rpx;
			color: #fff;
			text-align: center;

			.title {
				font-size: 40rpx;
				font-weight: bold;
				display: block;
				margin-bottom: 16rpx;
			}

			.subtitle {
				font-size: 28rpx;
				display: block;
				margin-bottom: 20rpx;
			}

			.school-picker {
				background-color: rgba(255, 255, 255, 0.2);
				border-radius: 50rpx;
				padding: 16rpx 24rpx;
				display: inline-flex;
				align-items: center;

				.picker-content {
					display: flex;
					align-items: center;

					text {
						font-size: 28rpx;
						margin-right: 10rpx;
					}
				}
			}
		}

		.content {
			flex: 1;
			padding: 30rpx;
			position: relative;

			.character-container {
				display: flex;
				justify-content: center;
				margin-bottom: 40rpx;

				.character {
					width: 200rpx;
					height: 200rpx;

					&.male {
						margin-right: -40rpx;
					}

					&.female {
						margin-left: -40rpx;
					}
				}
			}

			.function-intro {
				background-color: #6850f0;
				border-radius: 20rpx;
				padding: 30rpx;
				margin-bottom: 40rpx;
				box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);

				.intro-title {
					font-size: 32rpx;
					font-weight: bold;
					color: #333;
					display: block;
					margin-bottom: 20rpx;
				}

				.function-tags {
					display: flex;
					flex-wrap: wrap;
					gap: 16rpx;

					.tag {
						background-color: #f0f0f0;
						padding: 10rpx 20rpx;
						border-radius: 30rpx;
						font-size: 26rpx;
						color: #666;
					}
				}
			}

			.bubbles-container {
				position: relative;
				height: 400rpx;
				background-color: #6850f0;
				border-radius: 20rpx;

				.bubble {
					position: absolute;
					background-color: #fff;
					border-radius: 40rpx;
					padding: 16rpx 24rpx;
					box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
					max-width: 60%;
					transition: all 0.3s ease;

					&:active {
						transform: scale(0.95) rotateZ(0deg);
					}

					.bubble-text {
						font-size: 26rpx;
						color: #333;
					}
				}
			}
		}

		.footer {
			display: flex;
			padding: 20rpx 30rpx;
			background-color: #fff;
			border-top: 1rpx solid #f0f0f0;

			.action-btn {
				flex: 1;
				margin: 0 10rpx;
				background-color: #8a2be2;
				color: #fff;
				border-radius: 50rpx;
				display: flex;
				align-items: center;
				justify-content: center;

				uni-icons {
					margin-right: 10rpx;
				}

				&.primary {
					background-color: #1989fa;
				}
			}
		}

		.school-popup {
			background-color: #fff;
			border-top-left-radius: 20rpx;
			border-top-right-radius: 20rpx;
			padding: 30rpx;
			max-height: 70vh;

			.popup-header {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 30rpx;

				.popup-title {
					font-size: 32rpx;
					font-weight: bold;
					color: #333;
				}

				.cancel-btn {
					font-size: 28rpx;
					color: #666;
					background-color: transparent;
					border: none;
					padding: 0;
				}
			}

			.search-box {
				display: flex;
				align-items: center;
				background-color: #f5f5f5;
				border-radius: 50rpx;
				padding: 16rpx 24rpx;
				margin-bottom: 30rpx;

				uni-icons {
					margin-right: 10rpx;
				}

				input {
					flex: 1;
					font-size: 28rpx;
				}
			}

			.school-list {
				max-height: 50vh;

				.school-item {
					display: flex;
					justify-content: space-between;
					align-items: center;
					padding: 24rpx 0;
					border-bottom: 1rpx solid #f0f0f0;

					text {
						font-size: 28rpx;
						color: #333;
					}
				}
			}
		}
	}
</style>